import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const router = new VueRouter({
    routes: [
        {
            path: '/',
            name: 'login',
            redirect: '/index/background',
            component: () => import('../views/index/login.vue'),
            children: [
                {
                    path: '/index/background',
                    name: 'index/background',
                    component: () => import('../views/index/background.vue'),
                }
            ]
        },
        {
            path: '/home',
            name: 'home',
            redirect: '/users',
            component: () => import('../views/home.vue'),
            children: [
                {
                    path: '/users',
                    name: 'users',
                    component: () => import('../views/users.vue'),
                },
                {
                    path: '/roles',
                    name: 'roles',
                    component: () => import('../views/roles.vue'),
                },
                {
                    path: '/rights',
                    name: 'rights',
                    component: () => import('../views/rights.vue'),
                },
                {
                    path: '/goods',
                    name: 'goods',
                    component: () => import('../views/goods/goods.vue'),
                },
                {
                    path: '/goods/addGoods',
                    name: 'goods/addGoods',
                    component: () => import('../views/goods/addGoods.vue'),
                },
                {
                    path: '/params',
                    name: 'params',
                    component: () => import('../views/params.vue'),
                },
                {
                    path: '/categories',
                    name: 'categories',
                    component: () => import('../views/categories.vue'),
                },
                {
                    path: '/orders',
                    name: 'orders',
                    component: () => import('../views/orders.vue'),
                },
                {
                    path: '/reports',
                    name: 'reports',
                    component: () => import('../views/reports.vue'),
                },
                {
                    path: '/web3',
                    name: 'web3',
                    component: () => import('../views/web3.vue'),
                },
                {
                    path: '/ipfs',
                    name: 'ipfs',
                    component: () => import('../views/ipfs.vue'),
                }
            ]
        },
    ]
})

router.beforeEach((to, from, next) => {
    if (to.path === '/index/background') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/index/background')
    next()
})
export default router
